package org.example.service;

import com.google.api.client.util.Lists;
import org.example.command.OrderCmd;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

@Service
public class OrderCmdCache {

    private final BlockingQueue<OrderCmd> queue = new LinkedBlockingQueue<>();

    public boolean cache(OrderCmd cmd) {
        return queue.offer(cmd);
    }

    public int size() {
        return queue.size();
    }

    public List<OrderCmd> getAll() {
        List<OrderCmd> msgList = Lists.newArrayList();
        int count = queue.drainTo(msgList);
        if(count == 0) {
            return null;
        }else {
            return msgList;
        }
    }
}
